<?php

namespace App\Http\Controllers\Inspection;

use Illuminate\Http\Request;
use App\Models\Inspection\Line;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

/**
 * 线路控制器逻辑
 *
 * Date: 23/04/2018
 * @author George
 * @package App\Http\Controllers\Inspection
 */
class LineController extends Controller
{
    /**
     * 根据公司和项目ID获取所有线路
     *
     * Date: 23/04/2018
     * @author George
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
    	$this->validate($request, [
    		'company_id' => 'required',
    		'project_id' => 'required',
		]);

        $company_id = $request->get('company_id');
        $project_id = $request->get('project_id');
        // 获取未分配的线路
        $unallocated = $request->get('unallocated');

        $paginate = $request->get('paginate');
        $query = Line::query();
        $query->where('company_id', $company_id)->where('project_id', $project_id);

        if ($unallocated) {
			/**
			 * 获取已分配线路的ID集合
			 */
        	$allocated = DB::table('schedules')->where([
        		['company_id', '=', $company_id],
        		['project_id', '=', $project_id],
			])->whereDate('date', $unallocated)
				->whereNotNull('line_id')
				->pluck('line_id');
			/**
			 * 过滤已分配的线路
			 */
        	$query->whereNotIn('id', $allocated);
		}

        if ($paginate) {
            $collection = $query->paginate($paginate);
        } else {
            $collection = $query->get();
        }

        return success($collection);
    }

    /**
     * 创建巡检线路
     *
     * Date: 23/04/2018
     * @author George
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $attributes = $this->validate($request, [
            'project_id' => 'required',
            'name' => 'required',
            'mistiming' => 'required'
        ]);
        $attributes['company_id'] = Auth::user()->company_id;
        $line = Line::create($attributes);
        return stored($line);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Inspection\Line  $line
     * @return \Illuminate\Http\Response
     */
    public function show(Line $line)
    {
        return success($line);
    }

    /**
     * 更新线路信息
     *
     * Date: 23/04/2018
     * @author George
     * @param Request $request
     * @param Line $line
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, Line $line)
    {
        $attributes = $this->validate($request, [
            'name' => 'nullable',
            'mistiming' => 'nullable'
        ]);

        $line->update($attributes);
        return updated($line);
    }

    /**
     * 删除线路
     *
     * Date: 23/04/2018
     * @author George
     * @param Line $line
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(Line $line)
    {
        $line->delete();
        return deleted();
    }
}
